﻿using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace CWM.Skinn
{
    public static partial class SkinnEditorEx
    {

        public static GUIStyle CenteredText(Color color, int size = 12)
        {
            GUIStyle style = new GUIStyle
            {
                alignment = TextAnchor.MiddleCenter,
                fontSize = size
            };
            style.normal.textColor = color;
            return style;
        }

        public static int DisplayButtons(string[] buttons, GUIStyle style)
        {
            GUILayout.Label("");
            Rect rect = GUILayoutUtility.GetLastRect();
            int count = buttons.Length;
            for (int i = 0; i < count; i++)
            {
                Rect rectSegment = rect.GetRectX(i, count);
                if (GUI.Button(rectSegment, buttons[i], style != null ? style : GUIStyle.none)) return i;
            }
            return -1;
        }

        public static class BuiltInAssets
        {
            public static bool BuiltInTexturesFailed { get; private set; }

            private static Texture2D popupImage;
            public static Texture2D PopupImage { get { if (!popupImage) LoadTextures(); return popupImage; } }

            private static Texture2D helpImage;
            public static Texture2D HelpImage { get { if (!helpImage) LoadTextures(); return helpImage; } }

            private static Texture2D prefabImage;
            public static Texture2D PrefabImage { get { if (!prefabImage) LoadTextures(); return prefabImage; } }

            private static void LoadTextures()
            {
                if (BuiltInTexturesFailed) return;
                popupImage = EditorGUIUtility.Load("icons/_Popup.png") as Texture2D;
                helpImage = EditorGUIUtility.Load("icons/_Help.png") as Texture2D;
                prefabImage = EditorGUIUtility.Load("icons/d_model large.png") as Texture2D;

                if (!popupImage) BuiltInTexturesFailed = true;
                if (!helpImage) BuiltInTexturesFailed = true;
                if (!prefabImage) BuiltInTexturesFailed = true;

                if(BuiltInTexturesFailed) Debug.LogWarning("Fail to load all build in textures.");
            }
       
            private static GUIContent editTransformInputs = null;
            public static GUIContent EditTransformInputs
            {
                get
                {
                    if (editTransformInputs != null) return editTransformInputs;
                    GUIContent content = EditorGUIUtility.IconContent("TransformTool");
                    if (content != null) editTransformInputs = new GUIContent(content) { text = "", tooltip = "Toggle Transformation Manual Input." };
                    else editTransformInputs = new GUIContent("t+", "Toggle Transformation Manual Input.");
                    return editTransformInputs;
                }
            }

            private static GUIContent editTransformInputsOn = null;
            public static GUIContent EditTransformInputsOn
            {
                get
                {
                    if (editTransformInputsOn != null) return editTransformInputsOn;
                    GUIContent content = EditorGUIUtility.IconContent("TransformTool On");
                    if (content != null) editTransformInputsOn = new GUIContent(content) { text = "", tooltip = "Toggle Transformation Manual Input." };
                    else editTransformInputsOn = new GUIContent("t-", "Toggle Transformation Manual Input.");
                    return editTransformInputsOn;
                }
            }

            private static GUIContent transformIcon = null;

            public static GUIContent TransformIcon
            {
                get
                {
                    if (transformIcon != null) return transformIcon;
                    GUIContent content = EditorGUIUtility.IconContent("Transform Icon");
                    if (content != null) transformIcon = new GUIContent(content) { text = "", };
                    else transformIcon = new GUIContent("");
                    return transformIcon;
                }
            }

            private static GUIContent meshIcon = null;
            public static GUIContent MeshIcon
            {
                get
                {
                    if (meshIcon != null) return meshIcon;
                    GUIContent content = EditorGUIUtility.IconContent("Mesh Icon");
                    if (content != null) { meshIcon = new GUIContent(content) { text = "", }; }
                    else meshIcon = new GUIContent("");
                    return meshIcon;
                }
            }

            private static GUIContent meshfilterIcon = null;
            public static GUIContent MeshfilterIcon
            {
                get
                {
                    if (meshfilterIcon != null) return meshfilterIcon;
                    GUIContent content = EditorGUIUtility.IconContent("MeshFilter Icon");
                    if (content != null) meshfilterIcon = new GUIContent(content) { text = "", };
                    else meshfilterIcon = new GUIContent("");
                    return meshfilterIcon;
                }
            }

            private static GUIContent meshRendererIcon = null;
            public static GUIContent MeshRendererIcon
            {
                get
                {
                    if (meshRendererIcon != null) return meshRendererIcon;
                    GUIContent content = EditorGUIUtility.IconContent("MeshRenderer Icon");
                    if (content != null) meshRendererIcon = new GUIContent(content) { text = "", };
                    else meshRendererIcon = new GUIContent("");
                    return meshRendererIcon;
                }
            }

            private static GUIContent smrIcon = null;
            public static GUIContent SMRIcon
            {
                get
                {
                    if (smrIcon != null) return smrIcon;
                    GUIContent content = EditorGUIUtility.IconContent("SkinnedMeshRenderer Icon");
                    if (content != null) smrIcon = new GUIContent(content) { text = "", };
                    else smrIcon = new GUIContent("");
                    return smrIcon;
                }
            }

            private static GUIContent umaIcon = null;
            public static GUIContent UMAIcon
            {
                get
                {
                    if (umaIcon != null) return umaIcon;
                    GUIContent content = EditorGUIUtility.IconContent("UMA Icon");
                    if (content != null) umaIcon = new GUIContent(content) { text = "", };
                    else umaIcon = new GUIContent("UMA");
                    return umaIcon;
                }
            }

            private static GUIContent lockIcon = null;
            public static GUIContent LockIcon
            {
                get
                {
                    if (lockIcon != null) return lockIcon;
                    GUIContent content = EditorGUIUtility.IconContent("LockIcon");
                    if (content != null) lockIcon = new GUIContent(content) { text = "", };
                    else lockIcon = new GUIContent("On");
                    return lockIcon;
                }
            }

            private static GUIContent lockIconOn = null;
            public static GUIContent LockIconOn
            {
                get
                {
                    if (lockIconOn != null) return lockIconOn;
                    GUIContent content = EditorGUIUtility.IconContent("LockIcon-On");
                    if (content != null) lockIconOn = new GUIContent(content) { text = "", };
                    else lockIconOn = new GUIContent("Off");
                    return lockIconOn;
                }
            }
        }
    }
}
